package com.example.zhinongbao.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.MessageAdapter;
import com.example.zhinongbao.model.Message;
import com.example.zhinongbao.presenter.MessagePresenter;

import java.util.ArrayList;
import java.util.List;

public class MessageFragment extends Fragment implements MessagePresenter.View {

    private RecyclerView rvMessages;
    private MessageAdapter adapter;
    private MessagePresenter presenter;
    private LinearLayout notificationPanel;
    private TextView tvEmpty;
    private LinearLayout messageInputContainer;
    private EditText etInput;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_message, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化Toolbar
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        toolbar.setTitle("消息"); // 设置标题为"消息"

        // 初始化视图
        notificationPanel = view.findViewById(R.id.notificationPanel);
        tvEmpty = view.findViewById(R.id.tv_empty);
        messageInputContainer = view.findViewById(R.id.messageInputContainer);
        rvMessages = view.findViewById(R.id.rvMessages);
        etInput = view.findViewById(R.id.etInput);

        // 设置空状态文本
        tvEmpty.setText("没有新消息");
        tvEmpty.setVisibility(View.GONE);

        // 初始化Presenter
        presenter = new MessagePresenter(this, requireContext());

        // 初始化RecyclerView
        rvMessages.setLayoutManager(new LinearLayoutManager(requireContext()));
        adapter = new MessageAdapter(new ArrayList<>());
        rvMessages.setAdapter(adapter);

        // 发送按钮点击事件
        ImageButton btnSend = view.findViewById(R.id.btnSend);
        btnSend.setOnClickListener(v -> {
            String content = etInput.getText().toString().trim();
            if (!content.isEmpty()) {
                presenter.sendMessage(content);
            }
        });

        // "去开启"按钮点击事件 - 打开通知设置
        Button btnOpenNotifications = view.findViewById(R.id.btnOpenNotifications);
        btnOpenNotifications.setOnClickListener(v -> {
            // 打开通知设置
            Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, requireContext().getPackageName());
            startActivity(intent);
        });

        // 加载历史消息
        presenter.loadMessages();
    }

    @Override
    public void showMessages(List<Message> messages) {
        if (messages != null && !messages.isEmpty()) {
            // 有消息时的显示逻辑
            tvEmpty.setVisibility(View.GONE);
            rvMessages.setVisibility(View.VISIBLE);
            adapter.setMessages(messages);
            scrollToBottom();
        } else {
            // 没有消息时的显示逻辑
            rvMessages.setVisibility(View.GONE);
            messageInputContainer.setVisibility(View.VISIBLE);
            tvEmpty.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onMessageSent() {
        etInput.setText("");
        // 消息发送后自动滚动到底部
        scrollToBottom();
    }

    @Override
    public void onMessageReceived(Message message) {
        // 如果有消息到达但当前为空状态，切换到消息列表
        if (tvEmpty.getVisibility() == View.VISIBLE) {
            tvEmpty.setVisibility(View.GONE);
            rvMessages.setVisibility(View.VISIBLE);
            messageInputContainer.setVisibility(View.VISIBLE);
        }

        adapter.addMessage(message);
        scrollToBottom();
    }

    private void scrollToBottom() {
        if (adapter.getItemCount() > 0) {
            rvMessages.post(() -> rvMessages.smoothScrollToPosition(adapter.getItemCount() - 1));
        }
    }
}